<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>EASY后台-配置</title>
    <link rel="icon" th:href="@{/static/images/favicon.ico}">
    <link rel="stylesheet" th:href="@{/static/css/common.css}">
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
</head>
<body>
<div class="easy-container">
    <form class="layui-form" id="dataForm" lay-filter="dataForm">
        <input type="text" name="id" th:field="${data.id}" class="layui-input" style="display: none">
        <div class="layui-row">
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">表名</label>
                    <div class="layui-input-block">
                        <input type="text" name="tableName" th:field="${data.tableName}" lay-verify="required"
                               placeholder="请输入" class="layui-input" disabled>
                    </div>
                </div>
            </div>
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">项目名</label>
                    <div class="layui-input-block">
                        <input type="text" name="projectName" th:field="${data.projectName}" lay-verify="required"
                               placeholder="请输入"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">版本号</label>
                    <div class="layui-input-block">
                        <input type="text" name="genVersion" th:field="${data.genVersion}" lay-verify="required"
                               placeholder="请输入"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">作者</label>
                    <div class="layui-input-block">
                        <input type="text" name="genAuthor" th:field="${data.genAuthor}" lay-verify="required"
                               placeholder="请输入"
                               class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">继承基类</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="extendBaseEntity" th:field="${data.extendBaseEntity}" lay-skin="switch" value="true" title="是|否">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">包名</label>
                    <div class="layui-input-block">
                        <input type="text" name="genPackage" th:field="${data.genPackage}" lay-verify="required"
                               placeholder="请输入"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">实体类</label>
                    <div class="layui-input-block">
                        <input type="text" name="genClass" th:field="${data.genClass}" lay-verify="required"
                               placeholder="请输入"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">功能名</label>
                    <div class="layui-input-block">
                        <input type="text" name="genFunction" th:field="${data.genFunction}" lay-verify="required"
                               placeholder="请输入"
                               class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">生成日志</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="genOperateLog" th:field="${data.genOperateLog}" lay-skin="switch" title="是|否" value="true">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">生成页面</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="genPage" th:field="${data.genPage}" lay-skin="switch" title="是|否" value="true">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">功能集</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="genAdd" th:field="${data.genAdd}" title="增" value="true">
                        <input type="checkbox" name="genRemove" th:field="${data.genRemove}" title="删" value="true">
                        <input type="checkbox" name="genEdit" th:field="${data.genEdit}" title="改" value="true">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">表格类型</label>
                    <div class="layui-input-block">
                        <select name="genPageTableType" th:field="${data.genPageTableType}" lay-verify="required">
                            <option value="PAGE">分页</option>
                            <option value="LIST">列表</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">表单类型</label>
                    <div class="layui-input-block">
                        <select name="genPageFormType" th:field="${data.genPageFormType}" lay-verify="required">
                            <option value="ONE">单列</option>
                            <option value="TWO">双列</option>
                            <option value="THREE">三列</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">打开方式</label>
                    <div class="layui-input-block">
                        <select name="genPageFormOpenType" th:field="${data.genPageFormOpenType}" lay-verify="required">
                            <option value="DIALOG">弹窗</option>
                            <option value="DRAWER">抽屉</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">路径</label>
                    <div class="layui-input-block">
                        <input type="text" name="genPath" th:field="${data.genPath}" lay-verify="required"
                               placeholder="请输入"
                               class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <table class="layui-hide" id="columnTable" lay-filter="columnTable"></table>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submitForm">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="cancelForm">取消</button>
            </div>
        </div>
    </form>
</div>
</body>
<script type="text/html" id="javaTypeSelect" th:inline="javascript">
    <select name="javaType" lay-filter="javaType" lay-append-to="body">
        <option value="">请选择</option>
        <option value="String" {{ d.javaType == 'String' ? 'selected' : '' }}>String</option>
        <option value="Boolean" {{ d.javaType == 'Boolean' ? 'selected' : '' }}>Boolean</option>
        <option value="Byte" {{ d.javaType == 'Byte' ? 'selected' : '' }}>Byte</option>
        <option value="Short" {{ d.javaType == 'Short' ? 'selected' : '' }}>Short</option>
        <option value="Integer" {{ d.javaType == 'Integer' ? 'selected' : '' }}>Integer</option>
        <option value="Long" {{ d.javaType == 'Long' ? 'selected' : '' }}>Long</option>
        <option value="Float" {{ d.javaType == 'Float' ? 'selected' : '' }}>Float</option>
        <option value="Double" {{ d.javaType == 'Double' ? 'selected' : '' }}>Double</option>
        <option value="BigDecimal" {{ d.javaType == 'BigDecimal' ? 'selected' : '' }}>BigDecimal</option>
        <option value="LocalDate" {{ d.javaType == 'LocalDate' ? 'selected' : '' }}>LocalDate</option>
        <option value="LocalDateTime" {{ d.javaType == 'LocalDateTime' ? 'selected' : '' }}>LocalDateTime</option>
        <option value="LocalTime" {{ d.javaType == 'LocalTime' ? 'selected' : '' }}>LocalTime</option>
        <option value="InputStream" {{ d.javaType == 'InputStream' ? 'selected' : '' }}>InputStream</option>
    </select>
</script>

<script type="text/html" id="pageHtmlTypeSelect" th:inline="javascript">
    <select name="pageHtmlType" lay-filter="pageHtmlType" lay-append-to="body">
        <option value="">请选择</option>
        <option value="INPUT" {{ d.pageHtmlType == 'INPUT' ? 'selected' : '' }}>输入框</option>
        <option value="INPUT_NUMBER" {{ d.pageHtmlType == 'INPUT_NUMBER' ? 'selected' : '' }}>数字输入框</option>
        <option value="TEXTAREA" {{ d.pageHtmlType == 'TEXTAREA' ? 'selected' : '' }}>文本域</option>
        <option value="SELECT" {{ d.pageHtmlType == 'SELECT' ? 'selected' : '' }}>选择框</option>
        <option value="CHECKBOX" {{ d.pageHtmlType == 'CHECKBOX' ? 'selected' : '' }}>复选框</option>
        <option value="RADIO" {{ d.pageHtmlType == 'RADIO' ? 'selected' : '' }}>单选框</option>
        <option value="DATE" {{ d.pageHtmlType == 'DATE' ? 'selected' : '' }}>日期</option>
        <option value="DATETIME" {{ d.pageHtmlType == 'DATETIME' ? 'selected' : '' }}>日期时间</option>
        <option value="TIME" {{ d.pageHtmlType == 'TIME' ? 'selected' : '' }}>时间</option>
        <option value="YEAR" {{ d.pageHtmlType == 'YEAR' ? 'selected' : '' }}>年份</option>
        <option value="SWITCH" {{ d.pageHtmlType == 'SWITCH' ? 'selected' : '' }}>开关</option>
        <option value="UPLOAD" {{ d.pageHtmlType == 'UPLOAD' ? 'selected' : '' }}>文件上传</option>
    </select>
</script>

<script type="text/html" id="pageTableQueryTypeSelect" th:inline="javascript">
    <select name="pageTableQueryType" lay-filter="pageTableQueryType" lay-append-to="body">
        <option value="">请选择</option>
        <option value="eq" {{ d.pageTableQueryType == 'eq' ? 'selected' : '' }}>=</option>
        <option value="like" {{ d.pageTableQueryType == 'like' ? 'selected' : '' }}>like</option>
        <option value="ne" {{ d.pageTableQueryType == 'ne' ? 'selected' : '' }}>!=</option>
        <option value="gt" {{ d.pageTableQueryType == 'gt' ? 'selected' : '' }}>></option>
        <option value="ge" {{ d.pageTableQueryType == 'ge' ? 'selected' : '' }}><=</option>
        <option value="lt" {{ d.pageTableQueryType == 'lt' ? 'selected' : '' }}><</option>
        <option value="le" {{ d.pageTableQueryType == 'le' ? 'selected' : '' }}><=</option>
        <option value="between" {{ d.pageTableQueryType == 'between' ? 'selected' : '' }}>between</option>
        <option value="in" {{ d.pageTableQueryType == 'in' ? 'selected' : '' }}>in</option>
    </select>
</script>

<script th:src="@{/static/js/jquery.js}"></script>
<script th:src="@{/static/layui/layui.js}"></script>
<script type="text/javascript" th:inline="javascript">
    layui.use(['element', 'layer', 'table', 'form'], function () {
        var element = layui.element;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;

        // table渲染
        var tableObj = table.render({
            elem: '#columnTable',
            cols: [
                [
                    {type: 'numbers', title: '序号', align: 'center'},
                    {field: 'columnName', title: '字段名', width: 120, align: 'center'},
                    {field: 'columnType', title: '字段类型', width: 100, align: 'center'},
                    {
                        field: 'columnPk', title: '主键', width: 60, align: 'center',
                        templet: function (row) {
                            return row.columnPk === true ? '是' : '否';
                        }
                    },
                    {
                        field: 'columnNotNull', title: '非空', width: 60, align: 'center',
                        templet: function (row) {
                            return row.columnNotNull === true ? '是' : '否';
                        }
                    },
                    {
                        field: 'columnIncrement', title: '自增', width: 60, align: 'center',
                        templet: function (row) {
                            return row.columnIncrement === true ? '是' : '否';
                        }
                    },
                    {field: 'javaField', title: '属性名', width: 120, align: 'center', edit: 'text'},
                    {field: 'javaFieldComment', title: '属性描述', width: 120, align: 'center', edit: 'text'},
                    {field: 'javaType', title: '属性类型', width: 140, align: 'center', templet: '#javaTypeSelect'},
                    {
                        field: 'pageTable', title: '表格', width: 60, align: 'center',
                        templet: function (row) {
                            return '<input type="checkbox" name="pageTable" lay-filter="pageTable" value="true" ' + (row.pageTable === true ? 'checked' : '') + '>';
                        }
                    },
                    {field: 'pageTableWidth', title: '表格列宽', width: 100, align: 'center', edit: 'text'},
                    {
                        field: 'pageForm', title: '表单', width: 60, align: 'center',
                        templet: function (row) {
                            return '<input type="checkbox" name="pageForm" lay-filter="pageForm" value="true" ' + (row.pageForm === true ? 'checked' : '') + '>';
                        }
                    },
                    {
                        field: 'pageFormRequired', title: '必填', width: 60, align: 'center',
                        templet: function (row) {
                            return '<input type="checkbox" name="pageFormRequired" lay-filter="pageFormRequired" value="true" ' + (row.pageFormRequired === true ? 'checked' : '') + '>';
                        }
                    },
                    {
                        field: 'pageTableQuery', title: '查询', width: 60, align: 'center',
                        templet: function (row) {
                            return '<input type="checkbox" name="pageTableQuery" lay-filter="pageTableQuery" value="true" ' + (row.pageTableQuery === true ? 'checked' : '') + '>';
                        }
                    },
                    {field: 'pageTableQueryType', title: '查询方式', width: 140, align: 'center', templet: '#pageTableQueryTypeSelect'},
                    {
                        field: 'pageTableOrder', title: '排序', width: 60, align: 'center',
                        templet: function (row) {
                            return '<input type="checkbox" name="pageTableOrder" lay-filter="pageTableOrder" value="true" ' + (row.pageTableOrder === true ? 'checked' : '') + '>';
                        }
                    },
                    {field: 'pageHtmlType', title: '组件', width: 140, align: 'center', templet: '#pageHtmlTypeSelect'},
                    {field: 'pageDictType', title: '字典', width: 140, align: 'center', edit: 'text'},
                    {field: 'orderNum', title: '排序号', width: 100, align: 'center', edit: 'text'}
                ]
            ],
            data: [[${data.columnList}]],
            page: false, // 不分页
            skin: 'grid', // 边框风格
            size: 'lg', // 尺寸
            even: true, // 隔行背景
            done: function (res, curr, count) {
                var options = this;

                // 获取当前行数据
                table.getRowData = function (tableId, elem) {
                    var index = $(elem).closest('tr').data('index');
                    return table.cache[tableId][index] || {};
                };

                // 选择框事件
                form.on('select(javaType)', function(obj) {
                    var value = obj.value;
                    var data = table.getRowData(options.id, obj.elem);
                    data.javaType = value;
                });
                form.on('select(pageTableQueryType)', function(obj) {
                    var value = obj.value;
                    var data = table.getRowData(options.id, obj.elem);
                    data.pageTableQueryType = value;
                });
                form.on('select(pageHtmlType)', function(obj) {
                    var value = obj.value;
                    var data = table.getRowData(options.id, obj.elem);
                    data.pageHtmlType = value;
                });
                form.on('checkbox(pageTable)', function (obj) {
                    var elem = obj.elem;
                    var data = table.getRowData(options.id, elem);
                    data.pageTable = elem.checked;
                });
                form.on('checkbox(pageForm)', function (obj) {
                    var elem = obj.elem;
                    var data = table.getRowData(options.id, elem);
                    data.pageForm = elem.checked;
                });
                form.on('checkbox(pageFormRequired)', function (obj) {
                    var elem = obj.elem;
                    var data = table.getRowData(options.id, elem);
                    data.pageFormRequired = elem.checked;
                });
                form.on('checkbox(pageTableQuery)', function (obj) {
                    var elem = obj.elem;
                    var data = table.getRowData(options.id, elem);
                    data.pageTableQuery = elem.checked;
                });
                form.on('checkbox(pageTableOrder)', function (obj) {
                    var elem = obj.elem;
                    var data = table.getRowData(options.id, elem);
                    data.pageTableOrder = elem.checked;
                });

                // 单元格普通编辑事件
                table.on('edit(columnTable)', function (obj) {
                    var update = {};
                    update[obj.field] = obj.value;
                    obj.update(update);
                });
            }
        });

        // 保存
        form.on('submit(submitForm)', function (form) {
            var formData = form.field;
            var tableData = table.getData('columnTable');
            var data = {
                id: formData.id,
                tableName: formData.tableName,
                projectName: formData.projectName,
                genAuthor: formData.genAuthor,
                genVersion: formData.genVersion,
                extendBaseEntity: formData.extendBaseEntity,
                genPackage: formData.genPackage,
                genClass: formData.genClass,
                genFunction: formData.genFunction,
                genPage: formData.genPage,
                genOperateLog: formData.genOperateLog,
                genAdd: formData.genAdd,
                genRemove: formData.genRemove,
                genEdit: formData.genEdit,
                genPageTableType: formData.genPageTableType,
                genPageFormType: formData.genPageFormType,
                genPageFormOpenType: formData.genPageFormOpenType,
                genPath: formData.genPath,
                columnList: tableData
            };

            $.ajax({
                url: '/genTable/edit',
                type: 'PUT',
                data: JSON.stringify(data),
                contentType: 'application/json',
                dataType: 'json',
                success: function (response) {
                    if (response.code === 200) {
                        // 关闭
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                        // 刷新
                        parent.location.reload();

                        layer.msg('修改成功', {icon: 1});
                    } else {
                        layer.msg(response.msg, {icon: 0});
                    }
                },
                error: function (xhr, status, error) {
                    layer.msg('请求失败', {icon: 0});
                }
            });
            return false;
        });

        // 取消
        $('#cancelForm').on('click', function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
            return false;
        });

    });
</script>
</html>